



How to print elements of a Stream in Java 8


Introduced in Java 8, the Stream API is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result.
The features of Java stream are –

A stream is not a data structure instead it takes input from the Collections, Arrays or I/O channels.
Streams don’t change the original data structure, they only provide the result as per the pipelined methods.
Each intermediate operation is lazily executed and returns a stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.

There are 3 ways to print the elements of a Stream in Java:

forEach()
println() with collect()
peek()

Below are the three ways to print the Stream in detail:

Stream forEach(Consumer action): This method performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect.
Syntax :


void forEach(Consumer<? super T> action)



Where, Consumer is a functional interface

and T is the type of stream elements.


Below is how to print elements of Stream using forEach() method:
Program 1:







 


 

 













// Java code to print the elements of Stream 
  
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the stream 
        Stream<String> stream = Stream.of("Geeks", "For", 
                                          "Geeks", "A", 
                                          "Computer", "Portal"); 
  
        // Print the stream 
        stream.forEach(s -> System.out.println(s)); 
    } 
} 



















Output:


Geeks

For

Geeks

A

Computer

Portal



Program 2: Using Short hand lambda expression







 


 

 













// Java code to print the elements of Stream 
  
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the stream 
        Stream<String> stream = Stream.of("Geeks", "For", 
                                          "Geeks", "A", 
                                          "Computer", "Portal"); 
  
        // Print the stream 
        stream.forEach(System.out::println); 
    } 
} 



















Output:


Geeks

For

Geeks

A

Computer

Portal



Program 3: This approach consumes the stream and makes it unavailable for future use. Hence the below code will throw an error since the stream is already consumed.







 


 

 













// Java code to print the elements of Stream 
  
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the stream 
        Stream<String> stream = Stream.of("Geeks", "For", 
                                          "Geeks", "A", 
                                          "Computer", "Portal"); 
  
        // Print the stream 
        stream.forEach(s -> System.out.println(s)); 
  
        // Since the stream has been already consumed 
        // this will throw exception 
        try { 
  
            // Print the stream 
            stream.forEach(s -> System.out.println(s)); 
        } 
  
        catch (Exception e) { 
  
            System.out.println("\nException: " + e); 
        } 
    } 
} 



















Output:


Geeks

For

Geeks

A

Computer

Portal



Exception: java.lang.IllegalStateException: 

stream has already been operated upon or closed




Using println() with collect(): This method collects the elements of the stream as a collector instance, for example as List. Hence the printing of List can be done easily using println() method.
Syntax:


System.out.println(stream.collect(Collectors.toList()));


Program 1:







 


 

 













// Java code to print the elements of Stream 
  
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the stream 
        Stream<String> stream = Stream.of("Geeks", "For", 
                                          "Geeks", "A", 
                                          "Computer", "Portal"); 
  
        // Print the stream 
        System.out.println(stream.collect(Collectors.toList())); 
    } 
} 



















Output:


[Geeks, For, Geeks, A, Computer, Portal]



Program 2: This approach also consumes the stream and makes it unavailable for future use. Hence the below code will throw an error since the stream is already consumed.







 


 

 













// Java code to print the elements of Stream 
  
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the stream 
        Stream<String> stream = Stream.of("Geeks", "For", 
                                          "Geeks", "A", 
                                          "Computer", "Portal"); 
  
        // Print the stream 
        System.out.println(stream.collect(Collectors.toList())); 
  
        // Since the stream has been already consumed 
        // this will throw exception 
        try { 
  
            // Print the stream 
            System.out.println(stream.collect(Collectors.toList())); 
        } 
  
        catch (Exception e) { 
  
            System.out.println("\nException: " + e); 
        } 
    } 
} 



















Output:


[Geeks, For, Geeks, A, Computer, Portal]



Exception: java.lang.IllegalStateException: 

stream has already been operated upon or closed




Stream peek(Consumer action): This method returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream. This is an intermediate operation i.e, it creates a new stream that, when traversed, contains the elements of the initial stream that match the given predicate.
Syntax :


Stream<T> 

    peek(Consumer<? super T> action)



Where, Stream is an interface and T is the type of 

stream elements. action is a non-interfering action

to perform on the elements as they are consumed 

from the stream and the function returns the new stream.


Program 1:







 


 

 













// Java code to print the elements of Stream 
  
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the stream 
        Stream<String> stream = Stream.of("Geeks", "For", 
                                          "Geeks", "A", 
                                          "Computer", "Portal"); 
  
        // Print the stream using peek() 
        // by providing a terminal operation count() 
        stream.peek(s -> System.out.println(s)).count(); 
    } 
} 



















Output:


Geeks

For

Geeks

A

Computer

Portal



Program 2: This approach do not consumes the stream. Hence the below code will not throw any error.







 


 

 













// Java code to print the elements of Stream 
  
import java.util.stream.*; 
  
class GFG { 
    public static void main(String[] args) 
    { 
  
        // Get the stream 
        Stream<String> stream = Stream.of("Geeks", "For", 
                                          "GeeksForGeeks", "A", 
                                          "Computer", "Portal"); 
  
        // Since the stream is not being consumed 
        // this will not throw any exception 
  
        // Print the stream 
        stream.filter(s -> s.startsWith("G")) 
            .peek(s -> System.out.println("Filtered value: " + s)) 
            .map(String::toUpperCase) 
            .peek(s -> System.out.println("Uppercase value :" + s)) 
            .count(); 
    } 
} 



















Output:


Filtered value: Geeks

Uppercase value :GEEKS

Filtered value: GeeksForGeeks

Uppercase value :GEEKSFORGEEKS










RishabhPrabhuTechnical Content Engineer at GeeksForGeeksIf you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please Improve this article if you find anything incorrect by clicking on  the "Improve Article" button below.







 


 

 
Most popular in Java
 






 
More related articles in Java
 



 


 













